package com.xiaoyu.lingdian.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xiaoyu.lingdian.core.mybatis.page.Page;
import com.xiaoyu.lingdian.entity.CoreMap;
import com.xiaoyu.lingdian.service.CoreMapService;
import com.xiaoyu.lingdian.tool.RandomUtil;
import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;
import com.xiaoyu.lingdian.vo.CoreMapVO;

@Controller
@RequestMapping(value="/coreMap")
public class CoreMapController extends BaseController {
	
	/**
	 * 服务中心表
	 */
	@Autowired
	private CoreMapService coreMapService;
	
	/**
	 * 查询经纬度列表
	 * 
	 * @param response
	 */
	@RequestMapping(value = "/find/all", method = RequestMethod.POST)
	public void findMapList(HttpServletResponse response) {
		logger.info("[CoreMapController.findMapList]:begin findMapList.");
		List<CoreMap> list = coreMapService.findCoreMapByCnd(null, null, null);
		List<CoreMapVO> vos = new ArrayList<CoreMapVO>();
		CoreMapVO vo;
		for (CoreMap coreMap : list) {
			vo = new CoreMapVO();
			vo.convertPOToVO(coreMap);
			vos.add(vo);
		}
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "获取服务中心坐标列表成功！", vos), response);
		logger.info("[CoreMapController.findMapList]:end findMapList.");
	}

	/**
	* 获取列表<Page>
	* @param pageNum 页码
	* @param pageSize 页数
	* @return
	*/
	@RequestMapping(value="/find/by/cnd", method=RequestMethod.POST)
	public void findByCnd (Integer pageNum, Integer pageSize, HttpServletResponse response) {
		logger.info("[CoreMapController]:begin findByCnd");
		if (pageNum == null || pageNum == 0) {
			pageNum = 1;
		}
		if (pageSize == null || pageSize == 0) {
			pageSize = 10;
		}
		Page<CoreMap> page = coreMapService.findCoreMapPage(null, null, null, pageNum, pageSize);
		Page<CoreMapVO> pageVO = new Page<CoreMapVO>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());
		List<CoreMapVO> vos = new ArrayList<CoreMapVO>();
		CoreMapVO vo;
		for (CoreMap coreMap : page.getResult()) {
			vo = new CoreMapVO();
			vo.convertPOToVO(coreMap);
			vos.add(vo);
		}
		pageVO.setResult(vos);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "page列表获取成功!", pageVO),response);
		logger.info("[CoreMapController]:end findByCnd");

	}
	
	/**
	 * 添加服务中心坐标
	 * 
	 * @param crmapLongitude
	 * @param crmapLatitude
	 * @param crmapName
	 * @param crmapDesc
	 * @param response
	 */
	@RequestMapping(value="/add/map", method=RequestMethod.POST)
	public void addMap(String crmapLongitude, String crmapLatitude, String crmapName, String crmapDesc, HttpServletResponse response) {
		logger.info("[CoreMapController.addMap]:begin addMap");
		if (StringUtils.isBlank(crmapLongitude)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入经度！"), response);
			return;
		}
		if (StringUtils.isBlank(crmapLatitude)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入纬度！"), response);
			return;
		}	
		CoreMap coreMap = new CoreMap();
		coreMap.setCrmapLatitude(crmapLatitude);
		coreMap.setCrmapLongitude(crmapLongitude);
		List<CoreMap> mapList = coreMapService.findCoreMapByCnd(crmapLongitude, crmapLatitude, null);
		if (mapList != null && mapList.size() > 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "该经纬度已存在！"), response);
			return;
		}
		String uuid = RandomUtil.generateString(16);
		coreMap.setCrmapUuid(uuid);
		coreMap.setCrmapStatus(1);		
		coreMap.setCrmapName(crmapName);		
		coreMap.setCrmapDesc(crmapDesc);
		coreMapService.insertCoreMap(coreMap);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "添加服务中心经纬度成功！", uuid), response);
		logger.info("[CoreMapController.addMap]:end addMap");
	}

	/**
	 * 修改服务中心坐标
	 * 
	 * @param crmapUuid
	 * @param crmapLongitude
	 * @param crmapLatitude
	 * @param crmapName
	 * @param crmapDesc
	 * @param response
	 */
	@RequestMapping(value="/update/map", method=RequestMethod.POST)
	public void updateMap(String crmapUuid, String crmapLongitude, String crmapLatitude, String crmapName, 
			String crmapDesc, HttpServletResponse response) {
		logger.info("[CoreMapController.updateMap]:begin updateMap");
		if (StringUtils.isBlank(crmapUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "缺少crmapUuid参数！"), response);
			return;
		}
		if (StringUtils.isBlank(crmapLongitude)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入经度！"), response);
			return;
		}
		if (StringUtils.isBlank(crmapLatitude)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入纬度！"), response);
			return;
		}
		CoreMap coreMap = new CoreMap();
		coreMap.setCrmapUuid(crmapUuid);
		coreMap.setCrmapLatitude(crmapLatitude);
		coreMap.setCrmapLongitude(crmapLongitude);
		List<CoreMap> mapList = coreMapService.findCoreMapByCnd(crmapLongitude, crmapLatitude, crmapUuid);
		if (mapList != null && mapList.size() > 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "修改后的经纬度已存在！"), response);
			return;
		}
		coreMap.setCrmapName(crmapName);
		coreMap.setCrmapDesc(crmapDesc);
		coreMapService.updateCoreMap(coreMap);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "更新服务中心经纬度成功！"), response);
		logger.info("[CoreMapController.updateMap]:end updateMap");
	}

	/**
	 * 删除服务中心坐标
	 * 
	 * @param crmapUuid
	 * @param response
	 */
	@RequestMapping(value="/delete/one", method=RequestMethod.POST)
	public void deleteOne(String crmapUuid, HttpServletResponse response) {
		logger.info("[CoreMapController.deleteOne]:begin deleteOne");
		if (StringUtils.isBlank(crmapUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "缺少crmapUuid参数！"), response);
			return;
		}
		CoreMap coreMap = new CoreMap();
		coreMap.setCrmapUuid(crmapUuid);
		coreMapService.deleteCoreMap(coreMap);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除服务中心坐标成功！"), response);
		logger.info("[CoreMapController.deleteOne]:end deleteOne");
	}

	/**
	 * 批量删除服务中心坐标
	 * 
	 * @param crmapUuids
	 * @param response
	 */
	@RequestMapping(value="/delete/batch", method=RequestMethod.POST)
	public void deleteBatch(String crmapUuids, HttpServletResponse response) {
		logger.info("[CoreMapController.deleteBatch]:begin deleteBatch");
		if (StringUtils.isBlank(crmapUuids)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		String[] array = crmapUuids.split("\\|");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < array.length; i++) {
			list.add(array[i]);
		}
		if (list.size() == 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		coreMapService.deleteCoreMapByCnd(list);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除服务中心坐标成功！"), response);
		logger.info("[CoreMapController.deleteBatch]:end deleteBatch");
	}

	/**
	 * 查看服务中心坐标
	 * 
	 * @param crmapUuid
	 * @param response
	 */
	@RequestMapping(value="/views", method=RequestMethod.POST)
	public void viewsMap(String crmapUuid, HttpServletResponse response) {
		logger.info("[CoreMapController.viewsMap]:begin viewsMap");
		if (StringUtils.isBlank(crmapUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "缺少crmapUuid参数！"), response);
			return;
		}
		CoreMap coreMap = new CoreMap();
		coreMap.setCrmapUuid(crmapUuid);
		coreMap = coreMapService.getCoreMap(coreMap);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "查看服务中心坐标详情成功！", coreMap), response);
		logger.info("[CoreMapController.viewsMap]:end viewsMap");
	}
	
	/**
	 * 根据服务中心经纬度查询坐标
	 * 
	 * @param crmapLongitude
	 * @param crmapLatitude
	 * @param response
	 */
	@RequestMapping(value="/views/cnd", method=RequestMethod.POST)
	public void viewsMapByCnd(String crmapLongitude, String crmapLatitude, HttpServletResponse response) {
		logger.info("[CoreMapController.viewsMapByCnd]:begin viewsMapByCnd");
		if (StringUtils.isBlank(crmapLongitude)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入经度！"), response);
			return;
		}
		if (StringUtils.isBlank(crmapLatitude)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入纬度！"), response);
			return;
		}
		CoreMap coreMap = new CoreMap();
		coreMap.setCrmapLatitude(crmapLatitude);
		coreMap.setCrmapLongitude(crmapLongitude);
		List<CoreMap> mapList = coreMapService.findCoreMapByCnd(crmapLongitude, crmapLatitude, null);
		if (mapList != null && mapList.size() > 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "查看服务中心坐标详情成功！", mapList.get(0)), response);
			return;
		}
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "没有该经纬度坐标点"), response);
		logger.info("[CoreMapController.viewsMapByCnd]:end viewsMapByCnd");
	}
	
}